package com.zgw.his.emr.widget;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.e4.xwt.forms.medicalControl.IMedicalControl;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

import com.zgw.his.emr.model.DetailRecord;
import com.zgw.his.emr.model.TemplateRecord;
import com.zgw.his.emr.model.TextRecord;
import com.zgw.his.emr.suport.XwtSuport;

/**
 * 记录模板
 * 
 * @author Z.g.W
 * */
public class MedicalTemplate extends MedicalWidget {
	private Composite templateView;
	private List<MedicalDetail> childrens;
	private TemplateRecord record;

	public MedicalTemplate() {
	}

	public MedicalTemplate(TemplateRecord template) {
		this.record = template;
	}

	public Composite createControl(Composite parent) {
		try {
			templateView = (Composite) XwtSuport.buildUI(parent,// 使用xwt构建模板控件
					record.getTemplateUrl());
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<IMedicalControl> controls = new ArrayList<IMedicalControl>();
		// 查找Medical控件并填充至 controls
		putMedicalControl(templateView, controls);

		// 构建模板明细
		childrens = new ArrayList<MedicalDetail>();
		for (IMedicalControl control : controls) {
			if (control.getCode() == null // 控件标识为空  忽略该控件
					|| control.getCode().trim().equals(""))
				continue;
			DetailRecord detailRecord = findDetailByCode(control.getCode());
			if (detailRecord == null) {
				detailRecord = new DetailRecord(control.getCode());
				record.addChild(detailRecord);
			}
			MedicalDetail medicalDetail = new MedicalDetail(detailRecord);
			medicalDetail.initControl(control);
			childrens.add(medicalDetail);
		}
		return templateView;
	}

	// 刷新记录
	public void refresh_record() {
		if (childrens != null) {
			for (MedicalDetail c : childrens) {
				c.refresh_record();
			}
		}
	}

	// 刷新值显示
	public void refreshValue() {

	}

	private DetailRecord findDetailByCode(String code) {
		for (DetailRecord d : record.getDetails()) {
			if (code.equals(d.getCode())) {
				return d;
			}
		}
		return null;
	}

	public void dispose() {
		// TODO 注销模板
	}

	public Composite getControl() {
		return templateView;
	}

	@Override
	public void setActivate(boolean activate) {
		// TODO Auto-generated method stub

	}

	public List<MedicalDetail> getChildrens() {
		return childrens;
	}

	/*
	 * ========================================================================
	 * 工具方法
	 * ========================================================================
	 */
	// 查找Medical 控件并填 充至 container
	private void putMedicalControl(Composite parent,
			List<IMedicalControl> container) {
		if (parent == null || parent.isDisposed() || container == null)
			return;

		for (Control c : parent.getChildren()) {
			if (c instanceof IMedicalControl) {
				container.add((IMedicalControl) c);
			}
			if (c instanceof Composite) {
				putMedicalControl((Composite) c, container);
			}
		}
	}
}
